package com.xbongbong.saas.model.impl;

import com.alibaba.fastjson.JSONObject;
import com.xbongbong.paas.constant.PaasConstant;
import com.xbongbong.paas.domain.entity.PaasFormDataEntity;
import com.xbongbong.paas.elasticsearch.model.PaasEsModel;
import com.xbongbong.paas.elasticsearch.pojo.dto.InsertDTO;
import com.xbongbong.paas.elasticsearch.util.EsHelper;
import com.xbongbong.paas.enums.DelEnum;
import com.xbongbong.paas.enums.FieldTypeEnum;
import com.xbongbong.paas.enums.FlowStatusEnum;
import com.xbongbong.paas.enums.IndexTypeEnum;
import com.xbongbong.paas.help.subform.TransferSubFormHelper;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.CommentUtil;
import com.xbongbong.paas.toolbox.util.DateTimeUtil;
import com.xbongbong.paas.toolbox.util.DateUtil;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.saas.constant.BasicConstant;
import com.xbongbong.saas.constant.ParameterConstant;
import com.xbongbong.saas.constant.StringConstant;
import com.xbongbong.saas.domain.dao.OpportunityUserDao;
import com.xbongbong.saas.domain.entity.OpportunityUserEntity;
import com.xbongbong.saas.domain.entity.UserTeamEntity;
import com.xbongbong.saas.enums.business.SalesOpportunityEnum;
import com.xbongbong.saas.enums.subform.OpportunityTeamEnum;
import com.xbongbong.saas.enums.subform.PaymentSheetTeamEnum;
import com.xbongbong.saas.model.OpportunityUserModel;
import com.xbongbong.sys.domain.entity.UserEntity;
import org.apache.commons.collections4.CollectionUtils;
import org.elasticsearch.action.support.WriteRequest;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import static org.elasticsearch.index.query.QueryBuilders.boolQuery;
import static org.elasticsearch.index.query.QueryBuilders.termQuery;
import static org.elasticsearch.index.query.QueryBuilders.termsQuery;

/**
 * @author long.rao
 * @version v1.0
 * @date 2019/1/30 14:33
 * @since v1.0
 */
@Service("opportunityUserModel")
public class OpportunityUserModelImpl implements OpportunityUserModel {

    private static final Logger LOG = LoggerFactory.getLogger(OpportunityUserModelImpl.class);

    @Resource
    private OpportunityUserDao opportunityUserDao;
    @Resource
    private PaasEsModel paasEsModel;
    @Resource
    private EsHelper esHelper;
    @Resource
    private TransferSubFormHelper transferSubFormHelper;


    @Override
    public Integer insert(OpportunityUserEntity entity) throws XbbException {
        long now = DateTimeUtil.getInt();
        entity.setAddTime(now);
        entity.setUpdateTime(now);
        Integer result = opportunityUserDao.insert(entity);
        PaasFormDataEntity paasFormDataEntity = transferSubFormHelper.transferUserTeamToFormData(IndexTypeEnum.IDX_SAAS_OPPORTUNITY_TEAM, entity);
        InsertDTO saveDTO = paasEsModel.getSaveDTO(paasFormDataEntity, IndexTypeEnum.IDX_SAAS_OPPORTUNITY_TEAM);
        paasEsModel.insert(saveDTO);
        return result;
    }

    @Override
    public Integer update(OpportunityUserEntity entity) throws XbbException {
        long now = DateTimeUtil.getInt();
        entity.setUpdateTime(now);
        Integer result = opportunityUserDao.update(entity);

        PaasFormDataEntity paasFormDataEntity = transferSubFormHelper.transferUserTeamToFormData(IndexTypeEnum.IDX_SAAS_OPPORTUNITY_TEAM, entity);
        InsertDTO saveDTO = paasEsModel.getSaveDTO(paasFormDataEntity, IndexTypeEnum.IDX_SAAS_OPPORTUNITY_TEAM);
        paasEsModel.update(saveDTO);
        return result;
    }

    @Override
    public void insertBatch(List<OpportunityUserEntity> opportunityUserEntityList, boolean isImport) throws XbbException {
        opportunityUserDao.insertBatch(opportunityUserEntityList);

        List<PaasFormDataEntity> paasFormDataEntities = transferSubFormHelper.transferUserTeamToFormDataList(IndexTypeEnum.IDX_SAAS_OPPORTUNITY_TEAM, opportunityUserEntityList);

        List<InsertDTO> insertDTOList = new ArrayList<>();
        for (PaasFormDataEntity paasFormDataEntity : paasFormDataEntities) {
            insertDTOList.add(paasEsModel.getSaveDTO(paasFormDataEntity, IndexTypeEnum.IDX_SAAS_OPPORTUNITY_TEAM));
        }

        if (isImport) {
            paasEsModel.insertBatch(insertDTOList, WriteRequest.RefreshPolicy.NONE);
        } else {
            paasEsModel.insertBatch(insertDTOList);
        }

    }

    @Override
    public List<OpportunityUserEntity> findEntitys(Map<String, Object> param) {
        // TODO 临时测验代码，为了找到错误路径，找到后去除
        if (param.size() == 2 && param.containsKey("del") && param.containsKey("corpid")) {
            try {
                throw new XbbException(SystemErrorCodeEnum.API_ERROR_100002);
            } catch (XbbException e) {
                LOG.error(e.getMsg(), e);
            }
        }

        return opportunityUserDao.findEntitys(param);
    }

    @Override
    public Integer getEntitysCount(Map<String, Object> param) {
        return opportunityUserDao.getEntitysCount(param);
    }

    @Override
    public List findEntitysWithoutSub(Map<String, Object> param) {
        return null;
    }

    @Override
    public void opportunityUserInsert(UserEntity userEntity, Long opportunityId, int isMain) throws XbbException {
        if (userEntity == null) {
            return;
        }

        String userId = userEntity.getUserId();
        String corpid = userEntity.getCorpid();
        String userName = userEntity.getName() == null ? "" : userEntity.getName();
        String userAvatar = userEntity.getAvatar() == null ? "" : userEntity.getAvatar();

        Map<String, Object> param = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        CommentUtil.addToMap(param, "corpid", corpid);
        CommentUtil.addToMap(param, "opportunityId", opportunityId);
        CommentUtil.addToMap(param, "userId", userId);
        CommentUtil.addToMap(param, "del", 0);
        CommentUtil.addToMap(param, "pageNum", 1);
        CommentUtil.addToMap(param, "start", 0);

        long now = DateUtil.getInt();
        List<OpportunityUserEntity> opportunityUserList = findEntitys(param);
        if (opportunityUserList == null || opportunityUserList.size() < 1) {
            OpportunityUserEntity opportunityUser = new OpportunityUserEntity();
            opportunityUser.setCorpid(corpid);
            opportunityUser.setUserId(userId);
            opportunityUser.setUserName(userName);
            opportunityUser.setUserAvatar(userAvatar);
            opportunityUser.setDataId(opportunityId);
            opportunityUser.setDel(0);
            opportunityUser.setIsMain(isMain);
            opportunityUser.setAddTime(now);
            opportunityUser.setUpdateTime(now);
            insert(opportunityUser);
        } else {
            OpportunityUserEntity opportunityUser = opportunityUserList.get(0);
            if (!Objects.equals(opportunityUser.getIsMain(), 1)) {
                opportunityUser.setCorpid(corpid);
                opportunityUser.setUserId(userId);
                opportunityUser.setUserName(userName);
                opportunityUser.setUserAvatar(userAvatar);
                opportunityUser.setDataId(opportunityId);
                opportunityUser.setIsMain(isMain);
                opportunityUser.setDel(0);
                opportunityUser.setUpdateTime(now);
                update(opportunityUser);
            }
        }
    }

    @Override
    public List<String> getOpportunityUserIdList(String corpid, Long opportunityId, Integer isMain) {
        return opportunityUserDao.getOpportunityUserIdList(corpid, opportunityId, isMain);
    }

    @Override
    public List<OpportunityUserEntity> getOpportunityUserList(String corpid, Long opportunityId, Integer isMain) {
        Map<String, Object> param = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        param.put(StringConstant.CORPID, corpid);
        param.put(StringConstant.DEL, DelEnum.NORMAL.getDel());
        param.put(StringConstant.DATA_ID, opportunityId);
        param.put("isMain", isMain);
        return findEntitys(param);
    }

    @Override
    public OpportunityUserEntity getByKey(Long dataId, String corpid) {
        return opportunityUserDao.getByKey(dataId,corpid);
    }

    @Override
    public void save(OpportunityUserEntity opportunityUserEntity) throws XbbException{
        if (opportunityUserEntity.getId() == null){
            insert(opportunityUserEntity);
        }else {
            update(opportunityUserEntity);
        }
    }

    @Override
    public void disbandedTeamByCustomerId(String corpid, List<Long> customerIdIn) throws XbbException{
        BoolQueryBuilder boolQueryBuilder = boolQuery();
        boolQueryBuilder.filter(termQuery("corpid.keyword",corpid));
        boolQueryBuilder.filter(termsQuery(SalesOpportunityEnum.getEsAttr4Keyword(SalesOpportunityEnum.CUSTOMER_NAME),customerIdIn));
        boolQueryBuilder.filter(termsQuery(FieldTypeEnum.FLOW_STATUS.getAlias(), Arrays.asList(FlowStatusEnum.NOT_NEED_APPROVAL.getType(), FlowStatusEnum.PASS.getType())));
        List<PaasFormDataEntity> paasFormDataEntityList = esHelper.findByScroll(IndexTypeEnum.IDX_SAAS_OPPORTUNITY,boolQueryBuilder,PaasFormDataEntity.class,Collections.singletonList(FieldTypeEnum.DATAID.getAlias()));
        if (paasFormDataEntityList.isEmpty()){
            return;
        }
        List<Long> idList = new ArrayList<>();
        for (PaasFormDataEntity entity : paasFormDataEntityList){
            idList.add(entity.getDataId());
        }
        Map<String,Object> map = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        map.put("corpid",corpid);
        map.put("dataIdIn",idList);
        map.put("del",0);
        List<OpportunityUserEntity> opportunityUserEntityList = findEntitys(map);
        opportunityUserDao.deleteBatchByOpportunityIdIn(idList,corpid);
        for (OpportunityUserEntity entity : opportunityUserEntityList){
            paasEsModel.delete(entity.getId(), corpid, IndexTypeEnum.IDX_SAAS_OPPORTUNITY_TEAM,entity.getDataId());
        }
    }

    /**
     * 查询协同团队基础列表
     *
     * @param map 参数
     * @return 团队
     */
    @Override
    public List<UserTeamEntity> findBaseEntitys(Map<String, Object> map) {
        // TODO 临时测验代码，为了找到错误路径，找到后去除
        if (map.size() == 2 && map.containsKey("del") && map.containsKey("corpid")) {
            try {
                throw new XbbException(SystemErrorCodeEnum.API_ERROR_100002);
            } catch (XbbException e) {
                LOG.error(e.getMsg(), e);
            }
        }
        return opportunityUserDao.findBaseEntitys(map);
    }

    @Override
    public void delete(OpportunityUserEntity opportunityUser) throws XbbException{
        opportunityUser.setDel(1);
        opportunityUser.setUpdateTime((long)DateUtil.getInt());
        opportunityUserDao.update(opportunityUser);
        paasEsModel.delete(opportunityUser.getId(), opportunityUser.getCorpid(), IndexTypeEnum.IDX_SAAS_OPPORTUNITY_TEAM,opportunityUser.getDataId());
    }

    @Override
    public Integer deleteBatchByOpportunityIdIn(List<Long> opportunityIdIn, String corpid) throws XbbException{
        Map<String,Object> map = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        map.put("corpid",corpid);
        map.put("del",0);
        map.put("opportunityIdIn",opportunityIdIn);
        List<OpportunityUserEntity> opportunityUserEntityList = findEntitys(map);
        Integer result = opportunityUserDao.deleteBatchByOpportunityIdIn(opportunityIdIn, corpid);
        for (OpportunityUserEntity entity : opportunityUserEntityList){
            paasEsModel.delete(entity.getId(), corpid, IndexTypeEnum.IDX_SAAS_OPPORTUNITY_TEAM,entity.getDataId());
        }
        return result;
    }

    @Override
    public Map<Long, String> batchDeleteById(String corpid, List<Long> removeIdList) throws XbbException{
        Map<String,Object> map = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        map.put("corpid",corpid);
        map.put("del",0);
        map.put("idIn",removeIdList);
        List<OpportunityUserEntity> opportunityUserEntityList= findEntitys(map);
        opportunityUserDao.batchDeleteById(corpid,removeIdList);
        List<InsertDTO> insertDTOList = new ArrayList<>();
        Map<Long,String> opportunityUserMap = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        for (OpportunityUserEntity entity : opportunityUserEntityList){
            opportunityUserMap.put(entity.getDataId(),entity.getUserId());
            InsertDTO insertDTO = new InsertDTO();
            insertDTO.setEsId(IndexTypeEnum.IDX_SAAS_OPPORTUNITY_TEAM.getType() + "_" + corpid + "_" + entity.getId());
            insertDTO.setParent(corpid + "_" + entity.getDataId());
            insertDTO.setIndexTypeEnum(IndexTypeEnum.IDX_SAAS_OPPORTUNITY_TEAM);
            insertDTO.setIndex(IndexTypeEnum.IDX_SAAS_OPPORTUNITY_TEAM.getIndex());
            insertDTO.setType(IndexTypeEnum.IDX_SAAS_OPPORTUNITY_TEAM.getType());
            Map<String,Object> sourceMap = new HashMap<>();
            sourceMap.put(StringConstant.UPDATE_TIME, DateTimeUtil.getInt());
            sourceMap.put(StringConstant.DEL,DelEnum.DELETE.getDel());
            insertDTO.setSource(sourceMap);
            insertDTOList.add(insertDTO);
        }
        paasEsModel.updateBatch(insertDTOList);
        return opportunityUserMap;
    }

    @Override
    public List<Long> getDataIdList(String corpid, Integer isMain, List<String> userId) {
        return opportunityUserDao.getDataIdList(corpid, isMain, userId);
    }

    @Override
    public void batchUpdateByUserIdIn(String corpid, List<Long> dataIdIn, List<UserTeamEntity> teamEntityList, Integer isMain, Integer del) throws XbbException {
        opportunityUserDao.batchUpdateByIdIn(corpid, dataIdIn, isMain, del);
        List<InsertDTO> insertList = new ArrayList<>();
        for (UserTeamEntity userTeamEntity : teamEntityList) {
            InsertDTO insertDTO = new InsertDTO();
            insertDTO.setEsId(IndexTypeEnum.IDX_SAAS_OPPORTUNITY_TEAM.getType() + "_" + corpid + "_" + userTeamEntity.getId());
            insertDTO.setParent(corpid + "_" + userTeamEntity.getDataId());
            Map<String, Object> map = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
            if (Objects.nonNull(isMain)) {
                JSONObject object = new JSONObject();
                object.put(OpportunityTeamEnum.IS_MAIN.getDataAttr(), isMain);
                map.put(StringConstant.JSON_DATA, object);
            }
            if (Objects.nonNull(del)) {
                map.put(ParameterConstant.DEL, del);
            }
            map.put(FieldTypeEnum.UPDATETIME.getAlias(), DateTimeUtil.getInt());
            insertDTO.setSource(map);
            insertDTO.setIndexTypeEnum(IndexTypeEnum.IDX_SAAS_OPPORTUNITY_TEAM);
            insertList.add(insertDTO);
        }
        paasEsModel.updateBatch(insertList);
    }

    @Override
    public void batchUpdateByUserIdIn(String corpid, List<Long> dataIdIn, List<UserTeamEntity> teamEntityList, Integer isMain, Integer del, WriteRequest.RefreshPolicy refreshPolicy) throws XbbException {
        opportunityUserDao.batchUpdateByIdIn(corpid, dataIdIn, isMain, del);
        List<InsertDTO> insertList = new ArrayList<>();
        for (UserTeamEntity userTeamEntity : teamEntityList) {
            InsertDTO insertDTO = new InsertDTO();
            insertDTO.setEsId(IndexTypeEnum.IDX_SAAS_OPPORTUNITY_TEAM.getType() + "_" + corpid + "_" + userTeamEntity.getId());
            insertDTO.setParent(corpid + "_" + userTeamEntity.getDataId());
            Map<String, Object> map = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
            if (Objects.nonNull(isMain)) {
                JSONObject object = new JSONObject();
                object.put(OpportunityTeamEnum.IS_MAIN.getDataAttr(), isMain);
                map.put(StringConstant.JSON_DATA, object);
            }
            if (Objects.nonNull(del)) {
                map.put(ParameterConstant.DEL, del);
            }
            map.put(FieldTypeEnum.UPDATETIME.getAlias(), DateTimeUtil.getInt());
            insertDTO.setSource(map);
            insertDTO.setIndexTypeEnum(IndexTypeEnum.IDX_SAAS_OPPORTUNITY_TEAM);
            insertList.add(insertDTO);
        }
        paasEsModel.updateBatch(insertList, refreshPolicy);
    }

    @Override
    public OpportunityUserEntity getByUserIdOpportunityId(String corpid, String userId, Integer opportunityId) {
        return opportunityUserDao.getByUserIdOpportunityId(corpid, userId, opportunityId);
    }

    @Override
    public void deleteBatchUser(List<OpportunityUserEntity> opportunityUserEntityList, String corpid, Integer del) throws XbbException {
        if (CollectionUtils.isEmpty(opportunityUserEntityList)) {
            return;
        }
        List<Long> idList = new ArrayList<>();
        List<InsertDTO> insertDTOList = new ArrayList<>();
        Map<String,Object> map = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        map.put(StringConstant.DEL, del);
        map.put(StringConstant.UPDATE_TIME, DateTimeUtil.getInt());
        for (OpportunityUserEntity entity : opportunityUserEntityList){
            idList.add(entity.getId());
            InsertDTO insertDTO = new InsertDTO();
            insertDTO.setEsId(IndexTypeEnum.IDX_SAAS_OPPORTUNITY_TEAM.getType() + "_" + corpid + "_" + entity.getId());
            insertDTO.setParent(corpid + "_" + entity.getDataId());
            insertDTO.setIndexTypeEnum(IndexTypeEnum.IDX_SAAS_OPPORTUNITY_TEAM);
            insertDTO.setIndex(IndexTypeEnum.IDX_SAAS_OPPORTUNITY_TEAM.getIndex());
            insertDTO.setType(IndexTypeEnum.IDX_SAAS_OPPORTUNITY_TEAM.getType());
            insertDTO.setSource(map);
            insertDTOList.add(insertDTO);
        }
        deleteBatch(idList,corpid, del);
        paasEsModel.updateBatch(insertDTOList);
    }

    private Integer deleteBatch(List<Long> idList, String corpid, Integer del) {
        return opportunityUserDao.deleteBatch(idList, corpid, del);
    }

    @Override
    public void updateBatchCustomerId(List<OpportunityUserEntity> opportunityUserEntityList, String corpid) throws XbbException {
        opportunityUserDao.updateBatchCustomerId(opportunityUserEntityList,corpid);
        List<InsertDTO> insertDTOList = new ArrayList<>();
        for (OpportunityUserEntity entity : opportunityUserEntityList){
            Map<String,Object> map = new HashMap<>();
            JSONObject object = new JSONObject();
            object.put(OpportunityTeamEnum.CUSTOMER_ID.getDataAttr(),entity.getCustomerId());
            map.put(StringConstant.JSON_DATA, object);
            InsertDTO insertDTO = new InsertDTO();
            insertDTO.setEsId(IndexTypeEnum.IDX_SAAS_OPPORTUNITY_TEAM.getType() + "_" + corpid + "_" + entity.getId());
            insertDTO.setParent(corpid + "_" + entity.getDataId());
            insertDTO.setIndexTypeEnum(IndexTypeEnum.IDX_SAAS_OPPORTUNITY_TEAM);
            insertDTO.setIndex(IndexTypeEnum.IDX_SAAS_OPPORTUNITY_TEAM.getIndex());
            insertDTO.setType(IndexTypeEnum.IDX_SAAS_OPPORTUNITY_TEAM.getType());
            insertDTO.setSource(map);
            insertDTOList.add(insertDTO);
        }
        paasEsModel.updateBatch(insertDTOList);
    }

    @Override
    public Integer updateBatch(List<OpportunityUserEntity> list, String corpid) throws XbbException {
        Integer result = opportunityUserDao.updateBatch(list, corpid);
        List<PaasFormDataEntity> paasFormDataEntities = transferSubFormHelper.transferUserTeamToFormDataList(IndexTypeEnum.IDX_SAAS_OPPORTUNITY_TEAM, list);
        List<InsertDTO> insertList = new ArrayList<>();
        for (PaasFormDataEntity paasFormDataEntity : paasFormDataEntities) {
            insertList.add(paasEsModel.getSaveDTO(paasFormDataEntity, IndexTypeEnum.IDX_SAAS_OPPORTUNITY_TEAM));
        }
        paasEsModel.updateBatch(insertList);
        return result;
    }

    /**
     *
     * @Description: 编辑团队成员标签
     * @param opportunityUserEntity
     * @return: void
     * @Author: shen.yang
     * @Date: 2022/1/17  14:20
     */
    @Override
    public void editLabel(OpportunityUserEntity opportunityUserEntity) throws XbbException {
        updateFormTeam(opportunityUserEntity);
    }

    /**
     * 更新协同人可编辑权限
     *
     * @param opportunityUserEntity
     */
    @Override
    public void updateEditFormPer(OpportunityUserEntity opportunityUserEntity) throws XbbException {
        updateFormTeam(opportunityUserEntity);
    }


    private void updateFormTeam(OpportunityUserEntity opportunityUserEntity) throws XbbException {
        Long updateTime = DateTimeUtil.getInt();
        opportunityUserEntity.setUpdateTime(updateTime);
        opportunityUserDao.update(opportunityUserEntity);
        InsertDTO insertDTO = new InsertDTO();
        String corpid = opportunityUserEntity.getCorpid();
        insertDTO.setEsId(IndexTypeEnum.IDX_SAAS_OPPORTUNITY_TEAM.getType() + "_" + corpid + "_" + opportunityUserEntity.getId());
        insertDTO.setParent(corpid + "_" + opportunityUserEntity.getDataId());
        Map<String, Object> map = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        map.put("updateTime", updateTime);
        insertDTO.setSource(map);
        insertDTO.setIndexTypeEnum(IndexTypeEnum.IDX_SAAS_OPPORTUNITY_TEAM);
        paasEsModel.update(insertDTO);
    }

    /**
     * 批量更新团队的人员为协同人身份以及协同人的可编辑权限
     *
     * @param corpid
     * @param dataIdIn
     * @param teamEntityList
     * @param isMain
     * @param needThrow
     * @throws XbbException
     */
    @Override
    public void batchUpdateCoAndPerByUserIdIn(String corpid, List<Long> idIn, List<UserTeamEntity> teamEntityList, Integer isMain, boolean needThrow) throws XbbException {
        batchUpdateCoAndPerByUserIdIn(corpid, idIn, teamEntityList, isMain, needThrow, WriteRequest.RefreshPolicy.WAIT_UNTIL);
    }

    @Override
    public void batchUpdateCoAndPerByUserIdIn(String corpid, List<Long> idIn, List<UserTeamEntity> teamEntityList, Integer isMain, boolean needThrow, WriteRequest.RefreshPolicy refreshPolicy) throws XbbException {
        Integer formPermission = teamEntityList.get(BasicConstant.ZERO).getFormPermission();
        opportunityUserDao.batchUpdateCoAndPerByUserIdIn(corpid, idIn, isMain, formPermission);
        List<InsertDTO> insertList = new ArrayList<>();
        for (UserTeamEntity userTeamEntity : teamEntityList) {
            InsertDTO insertDTO = new InsertDTO();
            insertDTO.setEsId(IndexTypeEnum.IDX_SAAS_OPPORTUNITY_TEAM.getType() + "_" + corpid + "_" + userTeamEntity.getId());
            insertDTO.setParent(corpid + "_" + userTeamEntity.getDataId());
            Map<String, Object> map = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
            if (Objects.nonNull(isMain)) {
                JSONObject object = new JSONObject();
                object.put(PaymentSheetTeamEnum.IS_MAIN.getDataAttr(), isMain);
                map.put(StringConstant.JSON_DATA, object);
            }
            map.put(FieldTypeEnum.UPDATETIME.getAlias(), DateTimeUtil.getInt());
            insertDTO.setSource(map);
            insertDTO.setIndexTypeEnum(IndexTypeEnum.IDX_SAAS_OPPORTUNITY_TEAM);
            insertList.add(insertDTO);
        }
        paasEsModel.updateBatch(insertList, refreshPolicy, needThrow);
    }

}
